import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 用户信息
  const userInfo = ref(null)
  const token = ref('')
  const role = ref('') // client, lawyer, admin, finance, marketing, appraisal
  
  // 计算属性
  const isLoggedIn = computed(() => !!token.value)
  const isClient = computed(() => role.value === 'client')
  const isLawyer = computed(() => role.value === 'lawyer')
  const isAdmin = computed(() => role.value === 'admin')
  const isFinance = computed(() => role.value === 'finance')
  const isMarketing = computed(() => role.value === 'marketing')
  const isAppraisal = computed(() => role.value === 'appraisal')
  
  // 设置用户信息
  const setUserInfo = (info) => {
    userInfo.value = info
  }
  
  // 设置token
  const setToken = (newToken) => {
    token.value = newToken
  }
  
  // 设置角色
  const setRole = (newRole) => {
    role.value = newRole
  }
  
  // 登录
  const login = (userData) => {
    setUserInfo(userData.userInfo)
    setToken(userData.token)
    setRole(userData.role)
  }
  
  // 登出
  const logout = () => {
    userInfo.value = null
    token.value = ''
    role.value = ''
  }
  
  return {
    userInfo,
    token,
    role,
    isLoggedIn,
    isClient,
    isLawyer,
    isAdmin,
    isFinance,
    isMarketing,
    isAppraisal,
    setUserInfo,
    setToken,
    setRole,
    login,
    logout
  }
}) 